<?
/*
 *      messaging.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class Messaging
{
	protected $id;
	
	public function __construct($id)
	{
		$this->id = $id;
	}
	
	public function isPlayerInPhonebook($cid)
	{
		$stmt = Engine::get()->db->prepare('SELECT COUNT(*) FROM `contacts` WHERE `pid` = :pid AND `cid` = :cid');
		$stmt->bindValue(':pid', $this->id);
		$stmt->bindValue(':cid', $cid);
		$stmt->execute();
		return ($stmt->fetchColumn() != 0);
	}
	
	public function addContact($cid)
	{
		if (!$this->isPlayerInPhonebook($cid))
		{
			$stmt = Engine::get()->db->prepare('INSERT INTO `contacts` (`pid`, `cid`) VALUES (:pid, :cid);');
			$stmt->bindValue(':pid', $this->id);
			$stmt->bindValue(':cid', $cid);
			$stmt->execute();
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function removeContact($cid)
	{
		$stmt = Engine::get()->db->prepare('DELETE FROM `contacts` WHERE `pid` = :pid AND `cid` = :cid LIMIT 1;');
		$stmt->bindValue(':pid', $this->id);
		$stmt->bindValue(':cid', $cid);
		$stmt->execute();
		return $stmt->rowCount();
	}
	
	public function listContacts()
	{
		$stmt = Engine::get()->db->prepare('SELECT c.`cid` as `id`, u.`name`, (u.`last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND u.`logged`) as `online` FROM `contacts` as c LEFT JOIN `users` as u ON c.`cid` = u.`id` WHERE c.`pid` = :id;');
		$stmt->bindValue(':id', $this->id);
		$stmt->execute();
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function sendMessage($rid, $msg)
	{
		$stmt = Engine::get()->db->prepare('INSERT INTO `messages` (`sid`, `rid`, `message`) VALUES (:sid, :rid, :message);');
		$stmt->bindValue(':sid', $this->id);
		$stmt->bindValue(':rid', $rid);
		$stmt->bindValue(':message', strip_tags($msg));
		$stmt->execute();
		return true;
	}
	
	public function listInbox()
	{
		$stmt = Engine::get()->db->prepare('SELECT m.`id`, m.`sid`, u.`name`, m.`message` FROM `messages` AS m LEFT JOIN `users` AS u ON u.`id` = m.`sid` WHERE m.`rid` = :id;');
		$stmt->bindValue(':id', $this->id);
		$stmt->execute();
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function listSent()
	{
		$stmt = Engine::get()->db->prepare('SELECT m.`id`, m.`rid`, u.`name`, m.`message` FROM `messages` AS m LEFT JOIN `users` AS u ON u.`id` = m.`rid` WHERE m.`sid` = :id;');
		$stmt->bindValue(':id', $this->id);
		$stmt->execute();
		return $stmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function getMessage($msgid)
	{
		$stmt = Engine::get()->db->prepare('SELECT m.`id`, m.`sid`, m.`rid`, ur.`name` as rname, us.`name` as sname, m.`message` FROM `messages` AS m LEFT JOIN `users` AS ur ON ur.`id` = m.`rid` LEFT JOIN `users` AS us ON us.`id` = m.`sid` WHERE m.`id` = :msgid LIMIT 1;');
		$stmt->bindValue(':msgid',$msgid);
		$stmt->execute();
		if ($stmt->rowCount() != 1)
		{
			throw new Application_Exception('Wrong msg number specified');
		}
		$r = $stmt->fetch(PDO::FETCH_ASSOC);
		$stmt->closeCursor();
		if (($r['sid'] != $this->id) && ($r['rid'] != $this->id))
		{
			throw new Application_Exception('Wrong msg number specified');
		}
		$result = array();
		$result['message'] = $r['message'];
		if ($r['sid'] == $this->id)
		{
			$result['name'] = $r['rname'];
			$result['pid'] = $r['rid'];
			$result['in'] = false;
		}
		elseif ($r['rid'] == $this->id)
		{
			$result['name'] = $r['sname'];
			$result['pid'] = $r['sid'];
			$result['in'] = true;
		}
		return $result;
	}
}

?>
